# -*- coding: utf-8 -*-
# @Time:2021/05/19 23:32
# @Author:Cindy
import json

import mitmproxy.http
from mitmproxy import http
from mitmproxy.tools.main import mitmdump

from mitm import Counter


class Events:

    def request(self, flow: mitmproxy.http.HTTPFlow):
        """
            The full HTTP request has been read.
        """
        pass


    def response(self, flow: mitmproxy.http.HTTPFlow):
        """
            The full HTTP response has been read.
        """
        if 'https://stock.xueqiu.com/v5/stock/batch/quote.json?_t' \
                in flow.request.url and 'x=' in flow.request.url:
            base_data = json.loads(flow.response.text)
            # 修改股票名称
            base_data['data']['items'][0]['quote']['name'] = 'Cindy_中国平安'
            base_data['data']['items'][1]['quote']['name'] = 'Cindy_三一重工'
            # 修改股票涨跌幅
            base_data['data']['items'][0]['quote']['percent'] = '-9.99'
            base_data['data']['items'][1]['quote']['percent'] = '+9.99'

            flow.response.text = json.dumps(base_data)


addons = [
    Counter(),
    Events()
]

if __name__ == '__main__':
    mitmdump(['-p', '8080', '-s', __file__])
